List interaction
This example demonstrates how to implement interactive list items in the Scripting app using swipe gestures. By leveraging leadingSwipeActions and trailingSwipeActions, you can provide contextual actions such as marking a message as unread, deleting a message, or flagging it.
Overview
You will learn how to:
- Display a list of messages using a custom cell layout
- Implement swipe actions on both leading and trailing edges
- Configure swipe behavior (e.g. disabling full swipe)
- Use
Button,Label, andCirclefor interactive UI elements
Example Code
1. Define Message Data Type
2. Create a Custom Message Cell
Each message is rendered with a colored indicator (for unread status), sender name, and content using HStack and VStack.
3. Manage State and Actions
4. Construct the List with Swipe Actions
5. Present the View and Exit
Key Features
- leadingSwipeActions: Add actions triggered by swiping from the leading edge (left-to-right in LTR layouts).
- trailingSwipeActions: Add actions triggered by swiping from the trailing edge.
- allowsFullSwipe: When set to
false, prevents full swipe from automatically triggering the first action. - Button Roles: Use roles like
"destructive"to style buttons (e.g., red for delete). - tint: Customize button color for better visual context.
Use Cases
- Email/Messaging Scripts: Mark messages as read/unread, delete, archive, or flag.
- To-Do Lists: Complete or remove tasks with quick gestures.
- Custom Tools: Attach context-specific actions to list items.
Swipe actions provide an efficient and intuitive way for users to perform actions directly within list views, improving interaction speed and user experience.
